import threading
import speech_recognition as sr
import pyttsx3
from datetime import datetime, timedelta
import sqlite3
import time
import pocketsphinx
import vosk
import pyaudio
class ConferenceHost:
    def __init__(self):
        # 初始化语音识别和文本到语音转换引擎
        self.recognizer = pocketsphinx.Decoder()
        self.engine = pyttsx3.init()
        # 设置语音识别的持续时间
        self.duration = 5  # 5秒
        # 会议开始时间
        self.start_time = datetime.now()
        # 会议结束时间
        self.end_time = self.start_time + timedelta(minutes=30)
        # 会议记录
        self.record = []
        # 关键字列表
        self.keywords = ["小星同学","小新同学"]
        # 线程锁
        self.lock = threading.Lock()
        # 会议主题
        self.agenda = []

    ########主线方法########
    def run(self):
        try:
            self.speak('会议主持人已启动')

            # 多线程处理
            listener_thread = threading.Thread(target=self.listen_speech)
            
            listener_thread.start()
            
            listener_thread.join()
            
            
        except Exception as e:
            print(f"run 异常：{e}")
        
    # 监听用户语音 并对关键字进行响应
    def listen_speech(self):
        
        try:
            model_path = "E:\\work\\project_st\\vosk-model-small-cn-0.22"  # 替换为您的模型文件路径
            
            model = vosk.Model(model_path)
            # 初始化Recognizer
            self.recognizer = vosk.KaldiRecognizer(model, 16000)
            # 初始化PyAudio
            p = pyaudio.PyAudio()
            # 打开音频流
            self.stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=8000)
            self.stream.start_stream()
            
            while True:
                data = self.stream.read(4000)
                if self.recognizer.AcceptWaveform(data):
                    result = self.recognizer.Result()
                    if result:
                        result = result.replace(" ", "")
                        print(result)
                        
                        for keywords_i in self.keywords:
                            if keywords_i in result:
                                self.speak("我在")
                                
                                # 等待命令
                                # self.wait_command()
                                
                                # 获取后续命令
                        
                        if "会议结束" in result:  # 将识别结果转换为小写
                            self.end_meeting()
                            break
                else:
                    partial_result = self.recognizer.PartialResult()
                    # print(partial_result)
                
        except Exception as e:
            print(f"listen_speech 异常:{e}")
    
    
    
    #######调用的方法#########
    def end_meeting(self):
        print("会议结束。")
        self.speak("会议结束")
 
    def wait_command(self):
        while True:
            data = self.stream.read(4000)
            if self.recognizer.AcceptWaveform(data):
                command_result = self.recognizer.Result()
                if command_result:
                    command_result = command_result.replace(" ", "")
                    print(command_result)
                    if "结束会议" in command_result:
                        self.end_meeting()
                        break
                    elif "开始下一个议题" in command_result:
                        # 执行开始下一个议题的逻辑
                        print("开始下一个议题")
                        break
                    # 可以根据需要添加更多的指令判断和对应的逻辑

    
    def speak(self,text):
        self.engine.say(text)
        self.engine.runAndWait()
    
    def add_to_agenda(self, item):
        self.agenda.append(item)
        
            
# 使用示例
if __name__ == "__main__":
    host = ConferenceHost()
    host.add_to_agenda("议题1")
    # host.add_to_agenda("议题2")
    host.run()
